/**
 * 
 */
package com.jushu.storbox.entities;

import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.database.IBaseDao;
import com.jushu.storbox.database.IFileDao;
import com.huawei.sharedrive.sdk.android.model.response.FolderResponse;
import com.huawei.sharedrive.sdk.android.modelV2.request.Thumbnail;
import com.huawei.sharedrive.sdk.android.modelV2.response.FileInfoResponseV2;

/**
 *  File info domain object class
 *
 */
public class TeamSpaceFileInfo extends FileInfo{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4324540388502372154L;

	public void valueOf(FileInfoResponseV2 fileInfo) {
		setId(fileInfo.getId());
		setType(fileInfo.getType());
		setName(fileInfo.getName());
		setCreatedBy(fileInfo.getCreatedBy());
		setModifiedBy(fileInfo.getModifiedBy());
		setIsFile(1);
		setStatus(fileInfo.getStatus());
		setOwnerBy(fileInfo.getOwnerBy());
		setParent(fileInfo.getParent());
		setMd5(fileInfo.getMd5());
		setSha1(fileInfo.getSha1());
		setSize(fileInfo.getSize());
		setCreatedAt(fileInfo.getCreatedAt());
		setModifiedAt(fileInfo.getModifiedAt());
		setLoctPath("");
		setContentCreatedAt(fileInfo.getContentCreatedAt());
		setContentModifiedAt(fileInfo.getContentModifiedAt());
		setContentSyncState(Constant.SYNC_STATUS_TRUE);
		setTransStatus(Constant.DOWNLOAD_NON_UPDATE_DB);
		setOperationTime(Calendar.getInstance().getTimeInMillis());
		setLocalLastModifyTime(Calendar.getInstance().getTimeInMillis());
		setDescription(fileInfo.getDescription());
		setEtag(fileInfo.getEtag());
		setEncrypt(fileInfo.isEncrypt());
		setShare(fileInfo.isShare());
		setSharelink(fileInfo.isSharelink());
		setSync(false);
		List<Thumbnail> list = fileInfo.getThumbnailUrlList();
		if(list != null && !list.isEmpty()) {
			setThumbnailURL(list.get(0).getThumbnailUrl());
		}
		setCurrentUserId(ShareDriveApplication.getInstance().getWnerID());
	}
	
	public void valueOf(FolderResponse fileInfo) {
		throw new UnsupportedOperationException("file not support set folder infomation!");
	}

	@Override
	public void fromCursor(Cursor cursor) {
		setId(cursor.getString(cursor.getColumnIndex(IBaseDao.id)));
		setName(cursor.getString(cursor.getColumnIndex(IBaseDao.file_name)));
		setIsFile(cursor.getInt(cursor.getColumnIndex(IBaseDao.is_file)));
		setOwnerBy(cursor.getString(cursor.getColumnIndex(IBaseDao.owner_by)));
		setParent(cursor.getString(cursor.getColumnIndex(IBaseDao.parent_folder_id)));
		setMd5(cursor.getString(cursor.getColumnIndex(IBaseDao.file_md5)));
		setSize(cursor.getLong(cursor.getColumnIndex(IBaseDao.file_size)));
		setCreatedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.server_ctime)));
		setModifiedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.server_mtime)));
		setLoctPath(cursor.getString(cursor.getColumnIndex(IBaseDao.local_path)));
		setContentCreatedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.client_ctime)));
		setContentModifiedAt(cursor.getLong(cursor.getColumnIndex(IBaseDao.client_mtime)));
		setContentSyncState(cursor.getInt(cursor.getColumnIndex(IBaseDao.content_sync_status)));
		setStatus(cursor.getString(cursor.getColumnIndex(IBaseDao.static_status)));
		setTransStatus(cursor.getInt(cursor.getColumnIndex(IBaseDao.trans_status)));
		setOperationTime(cursor.getLong(cursor.getColumnIndex(IBaseDao.local_operation_time)));
		setLocalLastModifyTime(cursor.getLong(cursor.getColumnIndex(IBaseDao.local_last_modify_time)));
		setDescription(cursor.getString(cursor.getColumnIndex(IBaseDao.description)));
		setEtag(cursor.getString(cursor.getColumnIndex(IBaseDao.file_etag)));
		setSha1(cursor.getString(cursor.getColumnIndex(IBaseDao.file_sha1)));
		setEncrypt(cursor.getInt(cursor.getColumnIndex(IBaseDao.isencrypt)) == 1);
		setShare(cursor.getInt(cursor.getColumnIndex(IBaseDao.isshare)) == 1);
		setSharelink(cursor.getInt(cursor.getColumnIndex(IBaseDao.isshare_link)) == 1);
		setSync(cursor.getInt(cursor.getColumnIndex(IBaseDao.isSync)) == 1);
		setThumbnailURL(cursor.getString(cursor.getColumnIndex(IBaseDao.fileIcon)));
		setLocalIdentity(cursor.getString(cursor.getColumnIndex(IBaseDao.file_true_md5)));
		setCurrentUserId(cursor.getString(cursor.getColumnIndex(IFileDao.CURRENT_USER_ID)));
		setTeamSpaceId(cursor.getString(cursor.getColumnIndex(IFileDao.teamspace_id)));
	}

	@Override
	public ContentValues toContentValues() {
		ContentValues cv = super.toContentValues();
		cv.put(IBaseDao.id, getId());
		cv.put(IBaseDao.file_name, getName());
		cv.put(IBaseDao.is_file, 1);
		cv.put(IBaseDao.owner_by, getOwnerBy());
		cv.put(IBaseDao.parent_folder_id, getParent());
		cv.put(IBaseDao.file_size, getSize());
		cv.put(IBaseDao.server_ctime, getCreatedAt());
		cv.put(IBaseDao.server_mtime, getModifiedAt());
		cv.put(IBaseDao.local_path, "");
		cv.put(IBaseDao.trans_status, getTransStatus());
		cv.put(IBaseDao.client_ctime, getContentCreatedAt());
		cv.put(IBaseDao.client_mtime, getContentModifiedAt());
		cv.put(IBaseDao.content_sync_status, getContentSyncState());
		cv.put(IBaseDao.static_status, getStatus());
		cv.put(IBaseDao.trans_status_ref_cnt, 0);
		cv.put(IBaseDao.is_exist, 0);
		cv.put(IBaseDao.file_true_md5, getLocalIdentity());
		cv.put(IBaseDao.local_operation_time, 5446456);
		cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance().getTimeInMillis());
		cv.put(IBaseDao.isencrypt, isEncrypt());
		cv.put(IBaseDao.isshare, isShare());
		cv.put(IBaseDao.isshare_link, isSharelink());
		cv.put(IBaseDao.file_md5, getMd5());
		cv.put(IBaseDao.file_sha1, getSha1());
		cv.put(IBaseDao.teamspace_id, getTeamSpaceId());
		cv.put(IFileDao.CURRENT_USER_ID, ShareDriveApplication.getInstance().getWnerID());
		return cv;
	}
	
	public ContentValues getIdentityCV() {
		ContentValues cv = super.toContentValues();
		cv.put(IFileDao.file_true_md5, getLocalIdentity());
		return cv;
	}
	
}
